package org.ediba.siri.web.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ediba.siri.core.image.domain.ImageBackup;
import org.ediba.siri.core.image.service.ImageService;
import org.json.JSONException;
import org.springframework.web.bind.annotation.ResponseBody;

public class ImageBackupController extends Controller {
	
	private ImageService imageService;
	public void setImageService(ImageService imageService) {this.imageService = imageService;}
	
	@ResponseBody
	public Map<String,Object> postHandler(HttpServletRequest request, HttpServletResponse response, ImageBackupCommand command){
		
		String name = null;
		try {name = command.getData().getString("name");
		} catch (JSONException e) {e.printStackTrace();}
		
		ImageBackup imageBackup = this.imageService.backupCreate(name);

		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("success", true);
		_result.put("data", imageBackup);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> getHandler(HttpServletRequest request, HttpServletResponse response, ImageBackupCommand command){
				
		List<ImageBackup> imageBackups = null; 		
		
		if (command.getQuery() != null){
			if (command.getQuery().length > 0)
				imageBackups = this.imageService.backupRetrieve(command.getQuery()[0],command.getStart(),command.getLimit());
			else
				imageBackups = new ArrayList<ImageBackup>(0);
		}
		else if (command.getName() != null)
			imageBackups = this.imageService.backupRetrieve(command.getName(),command.getStart(),command.getLimit());
		else 
			imageBackups = this.imageService.backupRetrieve(command.getSort(),command.getDir(),command.getStart(),command.getLimit());
		
		Map<String,Object> _result = new HashMap<String,Object>(4);
		_result.put("data", imageBackups);
		_result.put("total", imageBackups.size());
		_result.put("success", true);
		_result.put("message", "OK");
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> putHandler(HttpServletRequest request, HttpServletResponse response, ImageBackupCommand command){
		
		Integer id = null;
		String name = null;
		try {
			id = command.getData().getInt("id");
			name = command.getData().getString("name");
		} catch (JSONException e) {e.printStackTrace();}
		
		ImageBackup imageBackup = this.imageService.backupRetrieve(id);
		if (name != null) 
			imageBackup.setName(name);
		imageBackup = this.imageService.backupUpdate(imageBackup);
			
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("success", true);
		_result.put("data", imageBackup);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> deleteHandler(HttpServletRequest request, HttpServletResponse response, ImageBackupCommand command){
		
		Integer id = null;
		try {id = command.getData().getInt("id");
		} catch (JSONException e) {e.printStackTrace();}
		
		boolean success = this.imageService.backupDelete(id);
		
		Map<String,Object> _result = new HashMap<String,Object>(1);
		_result.put("success", success);
		return _result;
	}

}
